﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BL;

namespace WebsiteBanHang
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        DanhMucLoaiSanPhamBL dmloaiSanPham = new DanhMucLoaiSanPhamBL();
        NguoiDungBL nguoiDung = new NguoiDungBL();

        protected void Page_Load(object sender, EventArgs e)
        {
            

            //DangNhap
                if ((bool)Session["DaDangNhap"] == false)
                {
                    txtMatKhau.Visible = true;
                    lb_MatKhau.Visible = true;
                    txtTenDangNhap.Visible = true;
                    lb_TenDangNhap.Visible = true;
                    btnDangNhap.Visible = true;
                    lbt_DangKy.Visible = true;
                    lbt_QuenMatKhau.Visible = true;

                    lb_Chao.Visible = false;
                    btn_Thoat.Visible = false;
                }
                else
                {
                    txtMatKhau.Visible = false;
                    lb_MatKhau.Visible = false;
                    txtTenDangNhap.Visible = false;
                    lb_TenDangNhap.Visible = false;
                    btnDangNhap.Visible = false;
                    lbt_DangKy.Visible = false;
                    lbt_QuenMatKhau.Visible = false;

                    lb_Chao.Text = "Chào " + Session["TaiKhoan"];
                    lb_Chao.Visible = true;
                    btn_Thoat.Visible = true;
                }
                LayDMLoaiSanPham();
                LoadDMSanPham();
        }

        protected void lbt_DangKy_Click(object sender, EventArgs e)
        {
            Response.Redirect("DangKy.aspx");
        }

        protected void btnThoat_Click(object sender, EventArgs e)
        {
            Session["DaDangNhap"] = false;
            Response.Redirect(Request.RawUrl);
        }

        protected void btnDangNhap_Click(object sender, EventArgs e)
        {
            bool ok = nguoiDung.KTDangNhap(txtTenDangNhap.Text, txtMatKhau.Text);
            if (ok == true)
            {
                Session["DaDangNhap"] = true;
                Session["TaiKhoan"] = txtTenDangNhap.Text;
                Response.Redirect("TrangDangNhapUser.aspx");
            }
            else
            {
                Session["DaDangNhap"] = false;
                Response.Redirect(Request.RawUrl);
            }
        }

        protected void LayDMLoaiSanPham()
        {
            List<DanhMucLoaiSanPhamDAL> dmlsp = new List<DanhMucLoaiSanPhamDAL>();
            dmlsp = dmloaiSanPham.LayDanhMucLoaiSanPham();
            for (int i = 0; i < dmlsp.Count; ++i)
            {
                //ddlloaiSanPham.Items.Add(dmlsp[i].TenLoaiSanPham);
                //ddlloaiSanPham.Items[i].Value = dmlsp[i].MaLoaiSanPham;
                //ddlloaiSanPham.Items[i].Text = dmlsp[i].TenLoaiSanPham;
            }
        }

        protected void LoadDMSanPham()
        {
            List<DanhMucLoaiSanPhamDAL> dmlsp = new List<DanhMucLoaiSanPhamDAL>();
            dmlsp = dmloaiSanPham.LayDanhMucLoaiSanPham();
            listview_dmsp.DataSource = dmlsp;
            listview_dmsp.DataBind();
        }
    }
}
